<?php

abstract class Dcmp_Module_Base {

    private $_template = NULL;
    private $_parameters = array();
    private $_block = NULL;
    private $_domain = NULL;
    
    protected $name = '';

    public function __construct($domain) {
        $this->_domain = $domain;
        $this->initialize();
    }
    
    protected function initialize(){
    	
    }

    protected function __($langKey) {
        return Kd_Localize_Helper::__($langKey);
    }

    protected function __f() {
        $args = func_get_args();
        return call_user_func_array(array('Kd_Localize_Helper', '__f'), $args);
    }
    
    protected function getDomain(){
    	return $this->_domain;
    }

    protected function getRequest() {
        return Kd_Web::getRequest();
    }

    protected function getResponse() {
        return Kd_Web::getResponse();
    }

    protected function getSession() {
        return Kd_Web::getSession();
    }

    protected function getVariables() {
        return Kd_Web::getVariables();
    }

    protected function getNavigation() {
        return Kd_Web::getPage()->getNavigation();
    }
    
    protected function getRouter(){
    	return $this->getDomain()->getRouter();
    }

    protected function getUrl($arguments = array(), $action = '*', $controller = '*', $component = '*') {
        return Kd_Web::getRouter()->buildUrl("$component/$controller/$action", $arguments);
    }

    public function getName() {
        return $this->name;
    }

    public function getParameters() {
        return $this->_parameters;
    }

    public function setParameters($value) {
        $this->_parameters = $value;
        return $this;
    }

    public function getDynamicParameter($name) {
        if (isset($this->_parameters[$name])) {
            $pv = $this->_parameters[$name];
            if(substr($pv, 0, 1) == '{' && substr($pv, -1) == '}'){
                $n = substr($pv, 1, -1);
                if ($this->getRequest()->hasParameter($n)) {
                    return $this->getRequest()->getParameter($n);
                }

                if ($this->getVariables()->issetData($n)) {
                    return $this->getVariables()->getData($n);
                }
            }

            return $pv;
        }

        return NULL;
    }

    public function getBlock() {
        return $this->_block;
    }

    public function setBlock($value) {
        $this->_block = $value;
        return $this;
    }

    protected function getTemplate() {
        if (is_null($this->_template)) {
            $this->_template = new Dcmp_Template($this->getDomain());
        }
        return $this->_template;
    }

    protected function getTemplatePath() {
        return DP_DESIGN .
        $this->getBlock()->getDomain() . DS .
        $this->getDomain()->getConfig()->getSetting('themeName') . DS . 'module' .
        DS . str_replace('_', DS, $this->getName()) . DS .
        str_replace('_', DS, $this->getBlock()->getBlockTemplateFile()) . '.phtml';
    }

    public function getValueByKey($key) {
        if (array_key_exists($key, $this->_parameters)) {
            return $this->_parameters[$key];
        }
    }

    abstract public function install();

    abstract public function uninstall();

    abstract public function buildConfig($form);

    abstract public function render();
}